import 'dart:convert';
import '../../generated/json/base/json_convert_content.dart';

class ApiResponseEntity<T> {
	bool? success = false;
	String? code = '';
	String? message = '';
	String? error = '';
	T? data;

	ApiResponseEntity();

	ApiResponseEntity.fromJson(Map<String, dynamic> map) {
		success = map['success'] as bool?;
		message = map['message'] as String?;
		code = map['code'] as String?;
		error = map['error'] as String?;
		if (map.containsKey('data')) {
			data = _generateOBJ(map['data']);
		}
	}

	T? _generateOBJ<O>(Object? json) {
		if (json == null) {
			return null;
		}
		if (T.toString() == 'String') {
			return json.toString() as T;
		} else if (T.toString() == 'Map<dynamic, dynamic>') {
			return json as T;
		} else {
			/// List类型数据由fromJsonAsT判断处理
			return JsonConvert.fromJsonAsT<T>(json);
		}
	}

	Map<String, dynamic> toJson() => $ApiResponseEntityToJson(this);

	Map<String, dynamic> $ApiResponseEntityToJson(ApiResponseEntity entity) {
		final Map<String, dynamic> data = <String, dynamic>{};
		data['success'] = entity.success;
		data['code'] = entity.code;
		data['message'] = entity.message;
		data['error'] = entity.error;
		data['data'] = entity.data;
		return data;
	}

	@override
	String toString() {
		return jsonEncode(this);
	}
}